<?php
namespace ITSociaal\Themes\Houtgrutter;

class WpRenderer extends WordPressBase implements RendererInterface
{
	/** @var string */
	protected $file;

	/**
	 * Set name of WP template file
	 *
	 * @param string $file
	 *
	 * @return self
	 * @throws \ITSociaal\Themes\Houtgrutter\Exception\InvalidArgumentException
	 */
	public function setFile($file)
	{
		$file = (string)$file;

		if (empty($file)) {
			throw new Exception\InvalidArgumentException(
				"\$file must not be empty"
			);
		}

		$this->file = $file;

		return $this;
	}

	/**
	 * Get name of WP template file
	 *
	 * @return string|null
	 * @throws \ITSociaal\Themes\Houtgrutter\Exception\RuntimeException
	 */
	public function getFile()
	{
		$file = $this->file;

		if (is_null($file)) {
			throw new Exception\RuntimeException(
				"file has not been set"
			);
		}

		return $file;
	}

	/**
	 * Render the WP template
	 *
	 * @return string
	 */
	public function render()
	{
		$file = $this->getFile();
		$wp   = $this->getWP();

		try {
			ob_start();
			$wp->load_template($file, false);
			return ob_get_clean();
		} catch (\Exception $ex) {
			ob_end_clean();
			throw $ex;
		}
	}
}

/* vi:set ts=4 sw=4 noet: */
